#pragma once
#include "Buffer.h"
#include <stdbool.h>
#include "HttpResponse.h"

//请求头键值对
struct RequestHeader
{
    char* key;
    char* value;
};

//当前的解析状态
enum HttpRequestState
{
    ParseReqLine,
    ParseReqHeaders,
    ParseReqBody,
    ParseReqDone
};

//定义http请求结构体
struct HttpRequest
{
    char* method;
    char* url;
    char* version;
    struct RequestHeader* reqHeaders;
    int reqHeadersNum;
    enum HttpRequestState curState;
};

//初始化
struct HttpRequest* HttpRequestInit();
//重置
void httpRequestReset(struct HttpRequest* req);
void httpRequestResetEx(struct HttpRequest* req);
void httpRequestDestroy(struct HttpRequest* req);
//获取处理状态
enum HttpRequestState HttpRequestState(struct HttpRequest* req);
//添加请求头
void httpRequestAddHeader(struct HttpRequest* req, const char* key, const char* value);
//根据key得到请求头的value
char* httpRequestGetHeader(struct HttpRequest* req, const char* key);
//解析请求行
bool parseHttpRequestLine(struct HttpRequest* req, struct Buffer* readBuf);
//解析请求头
bool parseHttpRequestHeader(struct HttpRequest* req, struct Buffer* readBuf);
//解析http请求协议
bool parseHttpRequest(struct HttpRequest*req, struct Buffer* readBuf, struct HttpResponse * response, struct Buffer* sendBuf, int socket);
//处理http请求协议
bool processHttpRequest(struct HttpRequest*req, struct HttpResponse * response);
// 解码
// to 存储解码之后的数据, 传出参数, from被解码的数据, 传入参数
void decodeMsg(char* to, char* from);
//获取文件类型
const char* getFileType(const char* name);
//发送文件
void sendFile(const char* fileName, struct Buffer* sendBuf, int cfd);
//发送目录
void sendDir(const char* dirName, struct Buffer* sendBuf, int cfd);